package cn.gemiman.daoyi.module.cms.dal.mysql.appsitecomment;

import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.mybatis.core.mapper.BaseMapperX;
import cn.gemiman.daoyi.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitecomment.vo.AppSiteCommentPageReqVO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitecomment.AppSiteCommentDO;
import cn.hutool.core.text.CharSequenceUtil;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 站点评论 Mapper
 *
 * @author 道一源码
 */
@Mapper
public interface AppSiteCommentMapper extends BaseMapperX<AppSiteCommentDO> {

    default PageResult<AppSiteCommentDO> selectPage(AppSiteCommentPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AppSiteCommentDO>()
                .eqIfPresent(AppSiteCommentDO::getUserType, reqVO.getUserType())
                .eqIfPresent(AppSiteCommentDO::getUserId, reqVO.getUserId())
                .likeIfPresent(AppSiteCommentDO::getUserNickname, reqVO.getUserNickname())
                .eqIfPresent(AppSiteCommentDO::getAppSiteId, reqVO.getAppSiteId())
                .eqIfPresent(AppSiteCommentDO::getAppId, reqVO.getAppId())
                .eqIfPresent(AppSiteCommentDO::getCategoryId, reqVO.getCategoryId())
                .eqIfPresent(AppSiteCommentDO::getArticleId, reqVO.getArticleId())
                .eqIfPresent(AppSiteCommentDO::getMessageId, reqVO.getMessageId())
                .eqIfPresent(AppSiteCommentDO::getTalkId, reqVO.getTalkId())
                .eqIfPresent(AppSiteCommentDO::getParentId, reqVO.getParentId())
                .eqIfPresent(AppSiteCommentDO::getCommentContent, reqVO.getCommentContent())
                .eqIfPresent(AppSiteCommentDO::getStatus, reqVO.getStatus())
                .eqIfPresent(AppSiteCommentDO::getApproveStatus, reqVO.getApproveStatus())
                .likeIfPresent(AppSiteCommentDO::getCreatorIp, reqVO.getCreatorIp())
                .likeIfPresent(AppSiteCommentDO::getCreatorCity, reqVO.getCreatorCity())
                .betweenIfPresent(AppSiteCommentDO::getCreateTime, reqVO.getCreateTime())
                .likeRight(CharSequenceUtil.isNotBlank(reqVO.getParentIds()), AppSiteCommentDO::getParentIds, reqVO.getParentIds())
                .orderByDesc(AppSiteCommentDO::getParentId)
                .orderByDesc(AppSiteCommentDO::getId));
    }

    @Select("select distinct id " +
            "from cms_app_site_comment casc " +
            "where casc.deleted = false " +
            "  and casc.parent_id > 0 " +
            "  and not exists(select 1 from cms_app_site_comment casc2 where casc2.deleted = false and casc2.id = casc.id)")
    List<Long> selectNoParents();

    @Update("update cms_app_site_comment set thumbs_up_times = thumbs_up_times + 1 where id = #{id}")
    void updateThumbsUpTimes(@Param("id") Long id);
}
